<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="50px">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="标题" prop="title">
						<el-input v-model="queryParams.title" placeholder="请输入标题:" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="状态:" prop="status">
						<el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择状态" clearable>
							<el-option v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
								:label="dict.label" :value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd" v-hasPermi="['system:post:add']">新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="标题" align="center" prop="title" />
			<el-table-column label="内容" header-align="center" prop="content">
				<template slot-scope="scope">
					<div v-html="scope.row.content"></div>
				</template>
			</el-table-column>
			<el-table-column label="排序号" align="center" prop="sortNumber" />
			<el-table-column label="状态" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.hotel_statistics_status" :value="scope.row.status" />
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px" v-loading="diLogloading">
				<el-form-item label="标题:" prop="title">
					<el-input v-model="form.title" placeholder="请输入标题" />
				</el-form-item>
				<el-form-item label="内容:" prop="content">
					<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 12}" placeholder="请输入内容"
						maxlength="2000" show-word-limit v-model="form.content">
					</el-input>
				</el-form-item>
				<el-form-item label="启用状态:" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
							:label="dict.value">
							{{dict.label}}
						</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="排序号:" prop="sortNumber">
					<el-input v-model="form.sortNumber" type="text" placeholder="请输入排序号" style="width: 200px;" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" v-if="!diLogloading">
				<el-button type="primary" @click="submitForm" :loading="btnLoading">{{form.configId?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		hotelReadingAdd,
		hotelReadingEdit,
		hotelReadingList,
		hotelReadingDelete,
		hotelReadingDetails
	} from "@/api/hotel/statistics"
	import QuillEdit from "@/components/Editor";
	export default {
		components: {
			QuillEdit
		},
		dicts: ['hotel_statistics_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogloading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					title: '',
					status: ''
				},
				// 表单参数
				form: {},
				btnLoading: false,
				// 表单校验
				rules: {
					title: [{
						required: true,
						message: "请输入标题",
						trigger: "blur"
					}],
					content: [{
						required: true,
						message: "请输入内容",
						trigger: "blur"
					}],
					sortNumber: [{
						required: true,
						message: "请输入排序号",
						trigger: "blur"
					}],
					status: [{
						required: true,
						message: "请选择状态",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				hotelReadingList(this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					title: undefined,
					status: '0',
					sortNumber: undefined,
					content: undefined
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.configId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "新增";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.postId || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.open = true;
				this.diLogloading = true
				this.title = "编辑";
				hotelReadingDetails(postId).then(response => {
					this.form.configId = response.data.configId
					for (let i in this.form) this.form[i] = response.data[i]
					this.diLogloading = false
				});
			},
			/** 提交按钮 */
			submitForm: function() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true
						if (this.form.configId != undefined) {
							hotelReadingEdit(this.form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							hotelReadingAdd(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.configId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return hotelReadingDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/post/export', {
					...this.queryParams
				}, `post_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
